/**
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-18
 */
var Controller = require("core/controller");
var net = require('util/net')
var log = require("util/log")
var constant = require('config/constant')
var storage = require('util/storage')
var navigate = require('util/navigate')
var server = require('config/server')
var router = require('config/router')
var user = require('module/userModule')
var notify = sm('do_Notification')
var Global = sm('do_Global')
var Algorithm = sm('do_Algorithm')
var do_Device=sm("do_Device")
//var do_External = sm('do_External')
Controller({
	name : 'login',
	config:{
		back:function(){
			//todo:添加setTimeout函数
			notify.confirm('是否确认退出当前应用',function(index){
				if(index === 1){
					Global.exit()
				}
			})
		}
	},
	mapping : {
		"tf_userName.text" : "username",
		"tf_password.text" : "password",
		"icon_cancle.visible":"canclevisi",
		"icon_look.visible":"icon_lookvisi",
	},
	ui:{
		test:'btn_login',
		icon_look:"icon_look",
		icon_cancle:"icon_cancle",
		tf_password:"tf_password",
		layout_icon:"layout_icon",
		headImage:"headImage"
	},
	data : {
		username : 'administrator',
		password : '888888',
		canclevisi:true,
		icon_lookvisi:true
	},
	event : {
		'btn_login' : {
			'touch' : function() {
				var self = this
				Algorithm.md5('string',this.data.password,function(password){
					self.login(self.data.username,password)
				})
				
			},
		},
		'icon_cancle' : {
			'touch' : function() {
				this.ui.tf_password.text=""
			}
		},
		'icon_look' : {
			'touch' : function() {
				if (this.ui.tf_password.password==true) {
					this.ui.tf_password.password=false
					this.ui.icon_look.iconCode="e6a6"
					this.ui.icon_look.iconColor="#28313EFF"
				}else{
					this.ui.tf_password.password=true
					this.ui.icon_look.iconCode="e6a5"
					this.ui.icon_look.iconColor="#A9A9A9FF"
				}
				
			}
		},
		'tf_password':{
			'textChanged' : function() {
				if (this.data.password.length>=1) {
					this.ui.test.bgColor="#28313eFF"
//					this.ui.icon_cancle.iconf  administrator
					this.ui.layout_icon.visible=true
				}else {
					this.ui.layout_icon.visible=false
				}
			}
		}
	},
	rules : {
	// TODO: 添加自定义数据验证
	},
	methods : {
		login : function(username,password) {
			log.info(password)
			net.send({
				server : server.organization.login,
				data : {
					username : username,
					password :password,
					usdeType:1,
					usdeCode:do_Device.getInfo().OS
					
				},
				success : function(data) {
					// 存储用户信息
					user.updateUserData(data)
					// TODO:添加临时存储
					navigate.redirectTo({
						view : router.index
					})
				},
				error : function(data) {
					notify.toast(data.reason)
				}
			})
		}
	},
	loaded : function() {
	}
})
